package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCDemo {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();) {
            //设置隔离级别
            connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            connection.setAutoCommit(false);//关闭自动提交(不会自动提交到数据库) 在JDBC中不用手动BEGIN 在执行首次DML语句会自动开启
            String sql = "INSERT INTO userinfo (username,password,nickname,age) " +
                    "VALUE(?,?,?,?) ";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, "王五");
            ps.setString(2, "888888");
            ps.setString(3, "小老五");
            ps.setInt(4, 45);
            int num = ps.executeUpdate();
            System.out.println(num > 0 ? "注册成功" : "注册失败");
            connection.commit();//手动提交事务到数据库
            //connection.rollback();//手动回滚事务
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
